<?php
/**
 *
 * Created by PhpStorm.
 * User: Earl <guo@seosite.cn>
 * Date: 2017/9/18
 * Time: 上午11:07
 * Updatetime:
 * updater：
 */

namespace App\Controller;

use lyf\Str;

class TestController extends BaseController
{

    public function demo()
    {

//        $randStr = Str::randString(1,'2');

//        var_dump($randStr);


        // @@Q27,866856038824809,A10*


//        $str = '@@X25,866856038824809,A10*';
        $str = '@@T25,866856038824809,A70*';
        $str = '@@V25,866856038824809,A12,6*';

        echo '<pre>';
        $str_hex_arr = $this->String2Hex($str);


        var_dump($str_hex_arr);
    }

    public function String2Hex($string)
    {
        $hex = 0;

        for ($i = 0; $i < strlen($string); $i++) {

            $y_hex = dechex(ord($string[$i]));


            $hex  = $this->hex_add($hex,$y_hex);

        }
        if (strlen($hex)==3){
            return strtoupper(substr($hex,1,2));
        }else{
            return strtoupper($hex);
        }
    }

    public function hex_add($a, $b)
    {

        $result = '';
        $chars = '0123456789abcdef';
        $len_a = strlen($a);
        $len_b = strlen($b);

        if ($len_a > $len_b) {
            $max = $len_a;
            $b = str_pad($b, $len_a, '0', STR_PAD_LEFT);
        } else {
            $max = $len_b;
            $a = str_pad($a, $len_b, '0', STR_PAD_LEFT);
        }

        $inc = $pos = 0;
        for ($i = $max - 1; $i >= 0; $i--) {
            $pos = hexdec($a[$i]) + hexdec($b[$i]) + $inc;
            $inc = floor($pos / 16);
            $result = $chars[$pos % 16] . $result;
        }
        if ($inc) {
            return $inc . $result;
        }
        return $result;
    }


    public function test()
    {
        $model = M('access');

        $model_role = M('role');

        $model_nave = M('nav');

        $list = $model_role->select();


        $nav = $model_nave->getField('id',true);

        $nav_str = implode(',',$nav);

        foreach ($list as $k=>$v){

            $insert_data = [
                'role_id'   =>  $v['id'],
                'nav_id'    =>  $nav_str
            ];

            $model->add($insert_data);
        }
    }


    public function demo2()
    {
        $id = 21;

//        $id = $param['id'];

        $accessModel = M('access');
        $navModel = M('nav');

        $accessInfo = $accessModel->where(['role_id'=>$id])->find();
        $nav_ids = $accessInfo['nav_id'];



        if (!empty($nav_ids)){

            $nav_list = $navModel->where(['id'=>['in',$nav_ids],'pid'=>0])->select();

            if (!empty($nav_list)){
                foreach ($nav_list as $k=>$v){
                    $next = $navModel->where(['id'=>['in',$nav_ids],'pid'=>$v['id']])->select();

                    $nav_list[$k]['_child'] = empty($next)?[]:$next;
                }
            }



        }

        $ret_data = [];


        var_dump($nav_list);


//        $accessinfo = $accessModel->where('role_id = ' . $id . '')->field('nav_id')->find();
//        $ids = '#' . $accessinfo['nav_id'] . '#';
//        $role_type = D('role')->where('id = %d', $id)->getField('type');
//        $navlist = $navModel->where('type = %d', 1)->select();
//        $navlist = $navModel->select();

        //        if ($role_type == 1) {
//            $navlist = $navModel->where('type = %d', 1)->select();
//        } else {
//            $navlist = $navModel->select();
////        }
//        foreach ($navlist as $key => $value) {
//            if ($value['controller'] == 'index' && $value['function'] == 'index') {
//                unset($navlist[$key]);
//            }
//        }

//        var_dump($navlist);
    }
}